package qc.module.ehs.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import qc.common.core.constants.QCAuthConstant;
import qc.common.core.exception.QCPromptException;
import qc.module.ehs.dto.investrecord.InvestRecordDto;
import qc.module.ehs.dto.investrecord.InvestRecordQueryConditionDto;
import qc.module.ehs.service.EhsInvestRecordService;
import qc.module.platform.dto.uac.UacUserInfoDto;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * InvestRecordController
 *
 * @author QuCheng Tech
 * @create 2024/10/29
 */
@RestController
@RequestMapping("/investrecord")
public class InvestRecordController {
    
    @Autowired
    private EhsInvestRecordService service;

    @Autowired
    HttpServletRequest request;

    @Autowired
    private RestTemplatePlatform restTemplatePlatform;

    /**
     * 查询安全生产资金投入记录
     * 
     * @param condition 查询条件DTO
     * @return List<InvestRecordDto>
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @RequestMapping(value = "/query",method = {RequestMethod.GET,RequestMethod.POST})
    public List<InvestRecordDto> query(@RequestBody InvestRecordQueryConditionDto condition) throws QCPromptException {
        return service.query(condition);
    }

    /**
     * 获取指定安全生产资金投入记录
     * 
     * @param id 安全生产资金投入记录ID
     * @return InvestRecordDto
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @GetMapping("/get")
    public InvestRecordDto getByParam(@RequestParam String id){
        return service.get(id);
    }

    /**
     * 获取指定安全生产资金投入记录
     *
     * @param id 安全生产资金投入记录ID
     * @return InvestRecordDto
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @GetMapping("/get/{id}")
    public InvestRecordDto getByPath(@PathVariable String id){
        return service.get(id);
    }

    /**
     * 新增记录
     * 
     * @param dto 新增对象DTO
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @RequestMapping(value = "/add",method = {RequestMethod.POST})
    public String add(@RequestBody InvestRecordDto dto) throws QCPromptException {
        HttpHeaders headers = new HttpHeaders();
        headers.add(QCAuthConstant.TOKEN_HEADER_KEY, request.getHeader(QCAuthConstant.TOKEN_HEADER_KEY));
        headers.add(QCAuthConstant.PROJECT_CODE_HEADER_KEY, request.getHeader(QCAuthConstant.PROJECT_CODE_HEADER_KEY));
        //根据token获取当前用户信息
        UacUserInfoDto userInfo = restTemplatePlatform.getUserInfo(headers);

        return service.add(dto, userInfo);
    }

    /**
     * 修改记录
     *
     * @param dto 修改对象DTO
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @RequestMapping(value = "/update",method = {RequestMethod.POST,RequestMethod.PUT})
    public String update(@RequestBody InvestRecordDto dto){
        return service.update(dto);
    }

    /**
     * 删除记录
     * 
     * @param id 记录ID
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @DeleteMapping("/delete")
    public String deleteByParam(@RequestParam String id){
        return service.delete(id);
    }

    /**
     * 删除记录
     *
     * @param id 记录ID
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/10/29
     */
    @DeleteMapping("/delete/{id}")
    public String deleteByPath(@PathVariable String id){
        return service.delete(id);
    }
}
